﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;

public partial class admin_addTeacher : System.Web.UI.Page
{
    public static string zzImage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Write("<script>alert('非法登录!!!');window.top.location='../Login.aspx';</script>");
            return;
        }

    }

    protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (fld_img.HasFile)
        {
            string path = Server.MapPath("~/upload/");
            string filestr = Path.GetExtension(fld_img.FileName).ToLower();
            string[] allowtype = { ".jpg", ".jpeg", ".png", "gif", ".bmp" };
            bool tf = false;
            for (int i = 0; i < allowtype.Length; i++)
            {
                if (filestr == allowtype[i])
                {
                    tf = true;
                }
            }
            if (tf)
            {
                try
                {
                    fld_img.PostedFile.SaveAs(path + fld_img.FileName);
                    Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('上传成功！')</script>");
                    Pic.ImageUrl = "~/upload/" + fld_img.FileName;

                }
                catch (Exception)
                {

                    Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('上传失败！')</script>");
                }

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('不支持给文件格式！')</script>");
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('请选择文件！')</script>");
        }
    }
    protected void btn_sure_Click(object sender, EventArgs e)
    {
        //判断是否上传了图片
        if (Pic.ImageUrl.ToString() == "")
        {
            Response.Write("<script>alert('未上传图片');</script>");
            return;
        }
        else
        {
            zzImage = Pic.ImageUrl.ToString();
        }
        string insertSql = "insert into tb_picture(pic_Title,pic_time,pic_address) values(@pic_Title,@pic_time,@pic_address)";
        OleDbParameter[] param = { 
                                     new OleDbParameter("@pic_Title",txt_title.Text),
                                     new OleDbParameter("@pic_time",DateTime.Now.ToString()),
                                     new OleDbParameter("@pic_address",zzImage)
                                     
                                 };

        int flag = OleDbHelper.ExecuteNonSql(insertSql, param);
        if (flag > 0)
        {
            //Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改成功！')</script>");
            Response.Write("<script>alert('添加成功');window.location.href='look.aspx'</script>");
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('修改失败，请稍候再试！')</script>");
        }
    }
    protected void btn_clear_Click(object sender, EventArgs e)
    {

    }
}